﻿using FileData.Currency;
using FileData.Utils.Currency;
using FileData.Utils.Currency.FileUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static FileData.DataUtils.ShortData;

namespace FileData.DataUtils
{
    class CurrentlyUtils
    {
       /// <summary>
       /// 获取规则文件的路径
       /// </summary>
       /// <param name="FilePath"></param>
       /// <returns></returns>
        public  string GetRuleFile(string FilePath)
        {
            string ShortInfoFile = new FloderNameHelper().GetFloderPath(FilePath) + "EnCoderHelper.xml";

            if (!FileHelper.FileExits(ShortInfoFile))
            {
                MessageHelper.Alert("未找到默认配置文件 EnCoderHelper.xml，请在此目录创建该文件");
            }

            return ShortInfoFile;
        }


        /// <summary>
        /// 读取规则文件的配置
        /// </summary>
        /// <param name="ShortInfoFile"></param>
        /// <returns></returns>
        public  List<ShortDataInfo> ReadDataInfo(string ShortInfoFile)
        {
            List<ShortDataInfo> ReplaceTextList = new List<ShortDataInfo>();
            foreach (string Line in HtmlHelper.GetHtmlValueList(FileHelper.ReadAllText(ShortInfoFile), "//replaceinfo"))
            {

                System.Console.WriteLine(Line);

                string RepForm = GetRule(Line, "//replaceform");
                string RepTo = GetRule(Line, "//replaceto");

                ReplaceTextList.Add(new ShortDataInfo { ReplaceForm = RepForm, ReplaceTo = RepTo });
            }

            return ReplaceTextList;
        }
        public void WriteDataInfo(String FilePath,List<ShortDataInfo> Data) {

            String Content = "";
            string ModeText = "<replaceinfo>\r\n<replaceform>{0}</replaceform>\r\n<replaceto>{1}</replaceto>\r\n </replaceinfo>";
        
            foreach (ShortDataInfo Single in Data) {
                Content += string.Format(ModeText, Single.ReplaceForm, Single.ReplaceTo);
            }
            FileHelper.WriteUTF8Text(FilePath,string.Format("<root>\r\n{0}\r\n</root>",Content));

        }
        /// <summary>
        /// 读取规则
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        private  string GetRule(string Text, string Xpath)
        {
            return HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(Text, Xpath));
        }

        /// <summary>
        /// 替换整个List里的内容
        /// </summary>
        /// <param name="RepList"></param>
        /// <param name="ReplaceForm"></param>
        /// <param name="ReplaceTo"></param>
        /// <returns></returns>
        public List<string> RepListHelper(List<string> RepList, string ReplaceForm, string ReplaceTo)
        {
            List<string> TempList = new List<string>();
            foreach (string Line in RepList)
            {
                if (Line == "")
                    continue;
                TempList.Add ( Line.Replace(ReplaceForm, ReplaceTo));
            }

            return TempList;

        }

        /// <summary>
        /// 替换整个List里的内容  增强模式，使用正则
        /// </summary>
        /// <param name="RepList"></param>
        /// <param name="ReplaceForm"></param>
        /// <param name="ReplaceTo"></param>
        /// <returns></returns>
        public List<string> RepListEnhanceHelper(List<string> RepList, string ReplaceForm, string ReplaceTo)
        {
            List<string> TempList = new List<string>();
            foreach (string Line in RepList)
            {
                if (Line == "")
                    continue;
                TempList.Add(TextHelper.ReplaceText(Line,ReplaceForm, ReplaceTo));
            }

            return TempList;

        }
        public struct ShortDataInfo
        {
            string replaceForm;
            string replaceTo;
            /// <summary>
            /// 需要替换的字符串  如cat
            /// </summary>
            public string ReplaceForm { get => replaceForm; set => replaceForm = value; }
            /// <summary>
            /// 替换为指定字符串 如：{0}
            /// </summary>
            public string ReplaceTo { get => replaceTo; set => replaceTo = value; }
        }

    }
}
